
function [] = test_downsampling()

fsin = 30000;
fsout = 6000;
scale = fsin / fsout;

func = @(ti) sin(2*pi*650*ti-pi/4) + sin(2*pi*1500*ti+pi/2);
tend = 1/500*2.4;

% build input func.
Tin = 1/fsin;
tin = (0:Tin:tend);
fin = func(tin);

% downsampling
[fout, tout] = downsampling(fin, fsin, fsout);

fout1 = decimate(fin, scale);

% calculate ideal reconstruction
forg = func(tout);

% compare in time

figure(1);
hold on;
plot(tout, forg, 'r');
plot(tin, fin, 'g');
plot(tout, fout, 'b');
plot(tout, fout1, 'm');
title('compare in time');
legend('ideal','in', 'out', 'decimate');

%% compare in fq

FIN = fftshift(abs(fft(fin)));
FOUT = fftshift(abs(fft(fout)));
FOUT1 = fftshift(abs(fft(fout1)));
s = size(FIN, 2) / size(FOUT, 2);
FORG = s*fftshift(abs(fft(forg)));
FOUT = s*FOUT;
FOUT1 = s*FOUT1;

fqout = fsout/2*linspace(-1, 1, size(FORG, 2));
fqin = (1/Tin)/2*linspace(-1, 1, size(FIN, 2));

figure(2);
hold on;
plot(fqout, FORG, 'r');
plot(fqin, FIN, 'g');
plot(fqout, FOUT, 'b');
plot(fqout, FOUT1, 'm');
title('compare in fq');
legend('ideal','in', 'out', 'decimate');

% error;

error_calc(fout, forg);
error_calc(fout1, forg);

% time

fsin = 30000;
fsout = 6000;

func = @(ti) sin(2*pi*500*ti-pi/4) + sin(2*pi*2000*ti+pi/2);
tend = 10;

% build input func.
Tin = 1/fsin;
tin = (0:Tin:tend);
fin = func(tin);

tic;

downsampling(fin, fsin, fsout);

display(sprintf('time: %d', toc))

end